//
//  AgreementAlertView.swift
//  EyeVisionForPatients
//
//  Created by QDSG on 2020/2/28.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit
import WebKit
import HandyJSON

enum AgreementType: Int {
    /// 疗患者知情书
    case informedLetter = 1
    /// 患者注册协议
    case patientRegister
    /// 医生注册协议
    case doctorRegister
}

struct AgreementViewModel: HandyJSON {
    var id: String = ""
    var proType: String = ""
    var title: String = ""
    var proText: String = ""

    static func inquiryAgreement(type: AgreementType, finish: @escaping (_ model: AgreementViewModel?, _ status: SEHttpStatus) -> Void) {

        SEHttpManager.shared.request(SECommonAPI.protocolDetail(type: type.rawValue), model: AgreementViewModel.self) { model, status in
            finish(model, status)
        }
    }
}

class AgreementAlertView: UIView {
    
    private var type = AgreementType.informedLetter
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        contentView.addSubview(webView)
        webView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.bottom.equalTo(sureBtn.snp.top).offset(-10)
        }
    }
    
    @IBOutlet private weak var maskBgView: UIView! {
        didSet {
            maskBgView.alpha = 0.0
        }
    }

    @IBOutlet private weak var contentView: UIView! {
        didSet {
            contentView.layer.cornerRadius = 16
            contentView.layer.masksToBounds = true
            contentView.transform = CGAffineTransform(scaleX: 0.0001, y: 0.0001)
        }
    }
    
    @IBOutlet private weak var titleLabel: UILabel!
    
    private lazy var webView = WKWebView()
    
    @IBOutlet private weak var sureBtn: UIButton! {
        didSet {
            sureBtn.setBackgroundImage(Asset.main.color.image(), for: .normal)
            sureBtn.setBackgroundImage(Asset.main.color.withAlphaComponent(0.36).image(), for: .highlighted)
            sureBtn.layer.cornerRadius = sureBtn.height * 0.5
            sureBtn.layer.masksToBounds = true
        }
    }
    
    @IBAction private func sureAction(_ sender: UIButton) {
        dismiss()
    }
    
    func show(in view: UIView? = nil, type: AgreementType) {
        self.type = type
        let superview = view == nil ? mainWindow : view!
        superview!.addSubview(self)
        self.frame = superview!.bounds
        UIView.animate(withDuration: 0.25) {
            self.maskBgView.alpha = 1.0
            self.contentView.transform = CGAffineTransform(scaleX: 1, y: 1)
        }
        
        loadData()
    }
    
    func dismiss() {
        UIView.animate(withDuration: 0.25, animations: {
            self.contentView.transform = CGAffineTransform(scaleX: 0.0001, y: 0.0001)
        }) { _ in
            self.maskBgView.alpha = 0.0
            self.removeFromSuperview()
        }
    }
}

extension AgreementAlertView {
    private func loadData() {
        AgreementViewModel.inquiryAgreement(type: type) { model, status in
            switch status {
            case .success:
                guard let m = model else { return }
                self.reloadData(m)
            case .auth(let msg), .failure(let msg):
                SEHUD.showError(in: nil, message: msg, position: .center)
            }
        }
    }
    
    private func reloadData(_ model: AgreementViewModel) {
        titleLabel.text = model.title
        webView.loadHTMLString(model.proText, baseURL: nil)
    }
}
